#pragma once

#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>

// Direct Input Macros
// Mouse input macros
#define BUTTONDOWN(name, key) (name.rgbButtons[key] & 0x80) 
#define BUTTONDOWNEVENT(current,last,key)(((current.rgbButtons[key] ^ last.rgbButtons[key]) & current.rgbButtons[key]) & 0x80)

// Keybaord input macros
#define KEYDOWN(name, key) (name[key] & 0x80)
#define KEYUP(name, key) (name[key] & 0x80)
#define DOWNEVENT(current,last,key)(((current[key] ^ last[key]) & current[key]) & 0x80)

class diManager
{
public:
	~diManager(void);

	// singleton stuff Called in Global Defines
	static diManager& getInstance() { static diManager pInstance; return pInstance;}

	bool initDirectInput(HINSTANCE hInst, HWND wndHandle);

	void shutdown(void);

	void getInput(void);

	int isButtonDown(int which);

	bool ButtonDownEvent(int which);

	bool isKeyDown(unsigned char which);

	bool KeyDownEvent(unsigned char which);

	void chckMouseBuffers(void);

	void checkBuffers(void);

	inline unsigned char getKeyboard(void) { return buffer[256]; }

	inline int getCurMouseX(void) { return mouseState.lX; }
	inline int getCurMouseY(void) { return mouseState.lY; }
	inline int getCurMouseWheel(void) { return mouseState.lZ; }

private:
	diManager(void);

	LPDIRECTINPUT8        m_lpDI;			// the direct input object
	LPDIRECTINPUTDEVICE8  m_lpDIDevice;		// the direct input device
	LPDIRECTINPUTDEVICE8  m_lpDIDeviceK;	// the direct input device

	DIMOUSESTATE mouseState;				// The current state of the mouse device
	DIMOUSESTATE lastState;					// The last state of the mouse device
	
	unsigned char buffer[256];				
	unsigned char LastBuffer[256];
	unsigned char countBuffer[256];

};
